package com.jindi.appuser.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * User: duanj
 * Date: 2018-01-16
 * Time: 11:23
 * Description:
 */
public class FileUtil {

    private Logger logger = LoggerFactory.getLogger(FileUtil.class);
    /**
     * 复制单个文件

     * @param oldPath String 原文件路径 如：c:/fqf.txt

     * @param newPath String 复制后路径 如：f:/fqf.txt

     * @return boolean

     */
    public void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在时

                InputStream inStream = new FileInputStream(oldPath); //读入原文件

                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ( (byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小

                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
            else{
                logger.info("文件不存在时！");
            }
        }
        catch (Exception e) {
            logger.info("复制单个文件操作失败！");
            e.printStackTrace();
        }
    }
}
